<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      :inline="true"
    >
      <!-- <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入部门名称"
          clearable
          style="width: 180px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item prop="fuzzy">
        <el-input
          v-model="queryParams.fuzzy"
          style="width: 250px"
          placeholder="仪器名称/仪器编号/仪器型号/仪器品牌"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="small"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="deptList"
      row-key="id"
      default-expand-all
      :tree-props="{ children: 'childList', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="name" label="位置名称" width="260" />
      <!-- <el-table-column prop="orderNum" label="排序" width="200" /> -->
      <el-table-column key="status" prop="status" label="位置状态" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 0 ? 'success' : 'danger'">{{
            scope.row.status === 0 ? "正常" : "停用"
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="创建日期"
        align="center"
        prop="createTime"
        width="200"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template> </el-table-column
      ><el-table-column
        label="停用日期"
        align="center"
        prop="stopDate"
        width="200"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stopDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="createUser" label="创建人" width="150" />
      <el-table-column prop="remark" label="备注" width="150" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="200px"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.parentId != null"
            v-hasPermi="['system:dept:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            v-hasPermi="['system:dept:add']"
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            >新增下级</el-button
          >
          <el-button
            v-if="scope.row.parentId != null"
            v-hasPermi="['system:dept:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加 -->
    <el-dialog :title="title" :visible.sync="open" width="620px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row :gutter="20">
          <el-col v-if="form.parentId !== 0 && form.parentId !== '0'" :span="12">
            <el-form-item label="上级位置" prop="parentId">
              <treeselect
                v-model="form.parentId"
                :options="deptOptions"
                :normalizer="normalizer"
                placeholder="选择上级位置"
                @select="changeId"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="位置名称:" prop="name">
              <el-input v-model="form.name" placeholder="请输入位置名称" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="位置状态:" prop="status">
              <el-radio-group v-model="status" @input="checkState">
                <el-radio :label="0">正常</el-radio>
                <el-radio :label="1">停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="停用日期:" prop="stopDate">
              <el-date-picker
                style="width: 100%"
                value-format="yyyy-MM-dd"
                v-model="form.stopDate"
                type="date"
                :disabled="msg"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="停用说明:" prop="remark">
              <el-input :disabled="msg" v-model="form.remark" maxlength="20" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改 -->
    <el-dialog :title="title" :visible.sync="modification" width="620px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row :gutter="20">
          <el-col v-if="form.parentId !== 0 && form.parentId !== '0'" :span="12">
            <el-form-item label="上级位置" prop="parentId">
              <treeselect
                v-model="form.parentId"
                :options="deptOptions"
                :normalizer="normalizer"
                placeholder="选择上级位置"
                @select="changeId"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="位置名称:" prop="name">
              <el-input v-model="form.name" placeholder="请输入位置名称" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="位置状态:" prop="status">
              <el-radio-group v-model="status" @input="checkState">
                <el-radio :label="0">正常</el-radio>
                <el-radio :label="1">停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="停用日期:" prop="stopDate">
              <el-date-picker
                style="width: 100%"
                value-format="yyyy-MM-dd"
                v-model="form.stopDate"
                type="date"
                :disabled="msg"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="停用说明:" prop="remark">
              <el-input :disabled="msg" v-model="form.remark" maxlength="20" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="modificationCancel">取 消</el-button>
        <el-button type="primary" @click="modificationConfirmation">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  roomList,
  updateRoom,
  addRoom,
  deleteById
} from "@/api/Roommanagement";
import Treeselect from "@riophae/vue-treeselect";
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: "Dept",
  components: { Treeselect },
  data() {
    return {
      status:0,
      //输入框禁用
      msg: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      deptList: [],
      // 部门树选项
      deptOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      modification:false,
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        name: "",
        status: ''
      },
      // 表单参数
      form: {
        name:'',
        status: "0", //默认选择正常
        remark:"",
        stopDate:"",
      },
      // 表单校验
      rules: {
        parentId: [
          { required: true, message: "上级位置不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "位置名称不能为空", trigger: "blur" }
        ],
        orderNum: [
          { required: true, message: "显示排序不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询部门列表 */
    getList() {
      this.loading = true;
      roomList(this.queryParams).then(response => {
        console.log(response.result);
        this.deptList = this.handleTree(response.result, "id");
        console.log(this.deptList);
        this.loading = false;
      });
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.childList && !node.childList.length) {
        delete node.childList;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.childList
      };
    },
    // 字典状态字典翻译
    statusFormat(row, column) {
      console.log(row);
      if (row.status === "0") {
        return "正常";
      } else {
        return "停用";
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    modificationCancel(){
      this.modification = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        status: undefined,
        parentId:undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      console.log(this.status);
      if (this.status == 0) {
        this.msg = true;
      }
      
      this.reset();
      if (row !== undefined) {
        this.form.parentId = row.id;
      }
      this.open = true;
      this.form.id = row.id;
      this.form.status = this.status
      console.log(this.form.status);
      
      this.title = "新增位置";
      roomList({ parentId: "", name: "", status: "" }).then(response => {
        this.deptOptions = this.handleTree(response.result, "id");
        console.log(this.deptOptions);
      });
    },
    changeId(e) {
      console.log(e);
      this.chooseId1 = e.id;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row);
      this.reset();
      if (row.status == 0) {
        this.msg = true;
        this.status = row.status
        this.form.status = row.status
      }
      if (row.status == 1) {
        this.msg = false;
        this.status = row.status
        this.form.status = row.status
        this.form.stopDate = row.createTime
        this.form.remark = row.remark
      }
      this.form.parentId=row.parentId;
      this.form.id = row.id;
      this.form.name = row.name;

      this.modification = true;
      this.title = "修改位置";
      if (row.id !== 0 && row.id !== "0") {
        roomList({ id: "", name: "", status: "" }).then(response => {
          this.deptOptions = this.handleTree(response.result, "id");
          console.log(this.deptOptions);
        });
      }
    },

    checkState(e) {
      console.log(1111);
      console.log(e);
      if (e == 0) {
        this.msg = true;
        this.form.status = e
        this.form.stopDate = ""
        this.form.remark = ""
      }
      if (e == 1) {
        this.msg = false;
        this.form.status = e
      }
    },
    //修改提交
    modificationConfirmation(){
      updateRoom(this.form).then(res => {
        if (res.code === 200) {
          this.$notify({
            title: "修改成功",
            message: res.message,
            type: "success"
          });
          this.modification = false;
          this.getList();
        }
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {{
            addRoom(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除名称为"' + row.name + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return deleteById({ id: row.id });
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    }
  }
};
</script>
